/**
 * 
 */
package com.cob.main;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import com.cob.view.IntroView;
import com.dalonedrau.engine.GameDrawable;
import com.dalonedrau.jogl.opengl.ui.ToolTip;
import com.dalonedrau.opengl.View;
import com.dalonedrau.opengl.ui.MessageWindow;
import com.dalonedrau.util.GlobalConstants;

/**
 * @author Donald
 */
public class Interface {
	/** the singleton instance. */
	private static Interface	instance;
	public static final int		VW_CONTROLS_BLOCKED	= 1 << 3;
	public static final int		VW_MSGWINDOW		= 1 << 1;
	public static final int		VW_PAUSED			= 1;
	public static final int		VW_TOOLTIP			= 1 << 2;
	/**
	 * Gets the one and only instance of {@link Interface}.
	 * @return {@link Interface}
	 */
	public static Interface getInstance() {
		if (Interface.instance == null) {
			Interface.instance = new Interface();
		}
		return Interface.instance;
	}
	private int		currentView;
	/** the current mouse location. can be null. */
	private Point	mouseLoc;
	private int		viewFlags	= 2;
	private View[]	views		= new View[1];
	/** Hidden constructor. */
	private Interface() {
		views[0] = new IntroView();
	}
	public void gameUpdate() {
		handleMouseOver();
	}
	public View getCurrentView() {
		return views[currentView];
	}
	/**
	 * Gets the current mouse location, possibly null.
	 * @return {@link Point}
	 */
	public Point getMouseLoc() {
		return mouseLoc;
	}
	public void handleKeyEvent(final KeyEvent e) {
		// IS GAME PAUSED

		// ARE CONTROLS BLOCKED
		if ((viewFlags & Interface.VW_CONTROLS_BLOCKED) == Interface.VW_CONTROLS_BLOCKED) {
			// ignore key presses
		} else {
			// IS THE MESSAGEWINDOW DISPLAYED?
			if ((viewFlags & Interface.VW_MSGWINDOW) == Interface.VW_MSGWINDOW) {
				// ignore any key presses except enter
				switch (e.getKeyCode()) {
				case KeyEvent.VK_ENTER:
					hideMessageWindow();
					break;
				case KeyEvent.VK_ESCAPE:
					COBApplet.getInstance().exit();
					break;
				}
			} else {
				if (views[currentView] != null) {
					views[currentView].handleKeyEvent(e);
				}
			}
		}
	}
	/**
	 * Checks the mouse position and changes the cursor when over a gui
	 * component.
	 */
	public void handleMouseOver() {
		if (mouseLoc != null) {
			// ARE CONTROLS BLOCKED
			if ((viewFlags & Interface.VW_CONTROLS_BLOCKED) == Interface.VW_CONTROLS_BLOCKED) {
				GameDrawable.setCursor(Cursor.WAIT_CURSOR);
			} else {
				GameDrawable.setCursor(Cursor.DEFAULT_CURSOR);
				float x = mouseLoc.x;
				float y = GlobalConstants.getInstance().getScreenHeight()
						- mouseLoc.y;
				if ((viewFlags & Interface.VW_MSGWINDOW) == Interface.VW_MSGWINDOW) {
					if (MessageWindow.getInstance().contains(x, y)) {
						System.out.println("momw");
						MessageWindow.getInstance().handleMouseOver(x, y);
					}
				} else {
					System.out.println("mov");
					if (views[currentView] != null) {
						views[currentView].handleMouseOver(x, y);
					}
				}
			}
			GameDrawable.switchCursor();
		}
	}
	public void handleMousePress(final MouseEvent e) {
		// ARE CONTROLS BLOCKED
		if ((viewFlags & Interface.VW_CONTROLS_BLOCKED) == Interface.VW_CONTROLS_BLOCKED) {
			// do nothing
		} else {
			if (mouseLoc != null) {
				float x = mouseLoc.x;
				float y = GlobalConstants.getInstance().getScreenHeight()
						- mouseLoc.y;
				if ((viewFlags & Interface.VW_MSGWINDOW) == Interface.VW_MSGWINDOW) {
					if (MessageWindow.getInstance().contains(x, y)) {
						MessageWindow.getInstance().handleMouseClick(x, y);
					}
				} else {
					if (views[currentView] != null) {
						views[currentView].handleMousePress(e, x, y);
					}
				}
			}
		}
	}
	/**
	 * Determines if the {@link Interface} has a specific flag set.
	 * @param flag the flag
	 * @return true if the object has the flag set; false otherwise
	 */
	public boolean hasViewFlag(final int flag) {
		return (viewFlags & flag) == flag;
	}
	public void hideMessageWindow() {
		if ((viewFlags & Interface.VW_MSGWINDOW) == Interface.VW_MSGWINDOW) {
			viewFlags -= Interface.VW_MSGWINDOW;
		}
	}
	/**
	 * Prepares the {@link View} for the rendering loop.
	 * @throws Exception if an error occurs
	 */
	public void prepareForRendering() throws Exception {
		if (views[currentView] != null) {
			views[currentView].prepareForRendering();
		}
		if ((viewFlags & Interface.VW_MSGWINDOW) == Interface.VW_MSGWINDOW) {
			System.out.println("should render mw");
			MessageWindow.getInstance().prepareForRendering();
		}
		if ((viewFlags & Interface.VW_TOOLTIP) == Interface.VW_TOOLTIP) {
			System.out.println("should render tt");
			ToolTip.getInstance().prepareForRendering();
		}
	}
	public void ResetPlayerInterface() {}
	public void setCurrentView(final int view) {
		currentView = view;
	}
	/**
	 * Sets the mouse location on screen.
	 * @param pt the {@link Point} to set
	 */
	public void setMouseLoc(final Point pt) {
		mouseLoc = pt;
	}
	public void showMessageWindow() {
		viewFlags += Interface.VW_MSGWINDOW;
	}
	public void showPopup(final String text) {
		MessageWindow.getInstance().showMessage(text);
		viewFlags += Interface.VW_MSGWINDOW;
	}
}
